package com.qiankun.tang.ui.fragment.home;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.common.library.listener.OnAppListener;
import com.common.library.tools.Logger;
import com.common.library.ui.fragment.BaseDrawRecyclerFragment;
import com.qiankun.tang.R;
import com.qiankun.tang.beans.ADBean;
import com.qiankun.tang.beans.VideoSynopsisBean;
import com.qiankun.tang.beans.items.HomeItem;
import com.qiankun.tang.ui.activity.PlayerActivity;
import com.qiankun.tang.ui.view.ADWidget;
import com.qiankun.tang.ui.view.adapter.home.HomeAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author txf
 * @Title
 * @package com.qiankun.video.ui.fragment
 * @date 2017/6/9 0009
 */
public class HomeFragment extends BaseDrawRecyclerFragment<HomeItem> implements OnAppListener.OnInteractionListener {
    private HomeAdapter mHomeAdapter;

    private List<String> urls;
    private List<String> titles2;

    private List<String> durls;
    private List<String> titles;

    private List<String> lurls;
    private List<String> ltitles;

    private void startPlayerActivity(String id) {
        startActivity(PlayerActivity.newIntent(id, getActivity()));
        getActivity().overridePendingTransition(R.anim.activity_zoomin, R.anim.activity_zoomout);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAdapter(mHomeAdapter = new HomeAdapter(getContext(), this));
        mHomeAdapter.setOnDrawTouchClick(this);
        initData();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        View view = mManager.findViewByPosition(0);
        if (view instanceof ADWidget) {
            ADWidget mADWidget = (ADWidget) view;
            if (hidden) {// 不在最前端界面显示
                mADWidget.stopPlayer();
            } else {// 重新显示到最前端
                mADWidget.startPlayer();
            }
        }
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void initData() {
        //轮播图数据
        String playUrl = "android.resource://" + getContext().getPackageName() + "/"
                + R.raw.home;
        String uri1 = "res:///" + R.drawable.ad1;
        String uri2 = "res:///" + R.drawable.ad2;
        String uri3 = "res:///" + R.drawable.ad3;
        String uri4 = "res:///" + R.drawable.ad4;

        urls = new ArrayList<>();
        urls.add(playUrl);
        urls.add(uri1);
        urls.add(uri2);
        urls.add(uri3);
        urls.add(uri4);
        titles2 = new ArrayList<>();
        String t1 = "加勒比海盗5";
        String t2 = "爱情，猝不及防";
        String t3 = "如此清雅的广告也是难得";
        String t4 = "士力架，遇见未来的自己";
        String t5 = "泰国的广告又上了一层楼啊";
        titles2.add(t1);
        titles2.add(t2);
        titles2.add(t3);
        titles2.add(t4);
        titles2.add(t5);

        //单个图片数据
        durls = new ArrayList<>();
        String duri1 = "res:///" + R.drawable.home_1;
        String duri2 = "res:///" + R.drawable.home_2;
        String duri3 = "res:///" + R.drawable.home_3;
        String duri4 = "res:///" + R.drawable.home_4;
        String duri5 = "res:///" + R.drawable.home_5;
        String duri6 = "res:///" + R.drawable.home_6;
        String duri7 = "res:///" + R.drawable.home_7;
        String duri8 = "res:///" + R.drawable.home_8;
        String duri9 = "res:///" + R.drawable.home_9;
        String duri10 = "res:///" + R.drawable.home_10;
        String duri11 = "res:///" + R.drawable.home_11;
        String duri12 = "res:///" + R.drawable.home_12;
        String duri13 = "res:///" + R.drawable.home_13;
        String duri14 = "res:///" + R.drawable.home_14;
        String duri15 = "res:///" + R.drawable.home_15;
        String duri16 = "res:///" + R.drawable.home_16;
        durls.add(duri1);
        durls.add(duri2);
        durls.add(duri3);
        durls.add(duri4);
        durls.add(duri5);
        durls.add(duri6);
        durls.add(duri7);
        durls.add(duri8);
        durls.add(duri9);
        durls.add(duri10);
        durls.add(duri11);
        durls.add(duri12);
        durls.add(duri13);
        durls.add(duri14);
        durls.add(duri15);
        durls.add(duri16);
        titles = new ArrayList<>();
        String title1 = "教你N种分手治愈法";
        String title2 = "泪奔, 我想要的未来是你幸福";
        String title3 = "美队这次带了个超萌萝莉";
        String title4 = "那些年剧中骗人的爱情混剪";
        String title5 = "男人们聚在一起也可以这么玩";
        String title6 = "女老师对付求爱男同学也是有一套";
        String title7 = "上海话嘻哈神曲《一个亿》";
        String title8 = "泰国保险感人广告";
        String title9 = "泰森欧巴来中国拍电影了";
        String title10 = "小伙实力演绎陪伴是最长情的告白";
        String title11 = "小栗旬的银魂造型酷到没朋友啊";
        String title12 = "小谢尔顿来袭";
        String title13 = "一首能让你放松的音乐";
        String title14 = "以一挑十，就问你怕不怕";
        String title15 = "遇见小时候的自己";
        String title16 = "真人版阴阳师造型没哭了";
        titles.add(title1);
        titles.add(title2);
        titles.add(title3);
        titles.add(title4);
        titles.add(title5);
        titles.add(title6);
        titles.add(title7);
        titles.add(title8);
        titles.add(title9);
        titles.add(title10);
        titles.add(title11);
        titles.add(title12);
        titles.add(title13);
        titles.add(title14);
        titles.add(title15);
        titles.add(title16);

        //列表图片数据
        lurls = new ArrayList<>();
        String lurl1 = "res:///" + R.drawable.hom_list_1;
        String lurl2 = "res:///" + R.drawable.hom_list_2;
        String lurl3 = "res:///" + R.drawable.hom_list_3;
        String lurl4 = "res:///" + R.drawable.hom_list_4;
        String lurl5 = "res:///" + R.drawable.hom_list_5;
        String lurl6 = "res:///" + R.drawable.hom_list_6;
        String lurl7 = "res:///" + R.drawable.hom_list_7;
        String lurl8 = "res:///" + R.drawable.hom_list_8;
        String lurl9 = "res:///" + R.drawable.hom_list_9;
        String lurl10 = "res:///" + R.drawable.hom_list_10;
        lurls.add(lurl1);
        lurls.add(lurl2);
        lurls.add(lurl3);
        lurls.add(lurl4);
        lurls.add(lurl5);
        lurls.add(lurl6);
        lurls.add(lurl7);
        lurls.add(lurl8);
        lurls.add(lurl9);
        lurls.add(lurl10);
        ltitles = new ArrayList<>();
        String ltitle1 = "宝宝第一次滑雪是什么反应";
        String ltitle2 = "超美沙画作品-大话西游";
        String ltitle3 = "还有叫“嗡”的和弦";
        String ltitle4 = "巨好听！电吉他演绎张国荣经典";
        String ltitle5 = "另类日料，你吃得下吗";
        String ltitle6 = "美女可不是这么追的";
        String ltitle7 = "你躲得开我的凝视吗";
        String ltitle8 = "女神要这样撩";
        String ltitle9 = "悬崖速降，惊心动魄";
        String ltitle10 = "这么懒又这么萌的猫咪我要了";
        ltitles.add(ltitle1);
        ltitles.add(ltitle2);
        ltitles.add(ltitle3);
        ltitles.add(ltitle4);
        ltitles.add(ltitle5);
        ltitles.add(ltitle6);
        ltitles.add(ltitle7);
        ltitles.add(ltitle8);
        ltitles.add(ltitle9);
        ltitles.add(ltitle10);
    }

    @Override
    protected void request(int pageIndex) {
        List<HomeItem> items = buildHomeItems();
        refreshData(items);
    }

    private List<HomeItem> buildHomeItems() {
        List<HomeItem> items = new ArrayList<>();
        //轮播图
        List<ADBean> adbeans = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            ADBean bean = new ADBean();
            bean.setId(i + "");
            bean.setType(0);
            if (i == 0) {
                bean.setType(1);
                bean.setSubtitle("死无对证");
            }
            bean.setImgUrl(urls.get(i));
            bean.setTitle(titles2.get(i));
            adbeans.add(bean);
        }
        items.add(new HomeItem(HomeItem.TYPE_0, adbeans));
        //分割线
        items.add(new HomeItem(HomeItem.TYPE_LINE, null));
        //单个图片
        for (int i = 0; i < 8; i++) {
            VideoSynopsisBean bean = new VideoSynopsisBean();
            bean.setImgUrl(durls.get(i));
            bean.setSubtitle("");
            bean.setTitle(titles.get(i));
            items.add(new HomeItem(HomeItem.TYPE_1, bean));
        }
        //列表图片
        List<VideoSynopsisBean> videoSynopsisBeans = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            VideoSynopsisBean bean = new VideoSynopsisBean();
            bean.setImgUrl(lurls.get(i));
            bean.setSubtitle(ltitles.get(i));
            bean.setTitle(ltitles.get(i));
            videoSynopsisBeans.add(bean);
        }
        items.add(new HomeItem(HomeItem.TYPE_2, videoSynopsisBeans));

        //单个图
        for (int i = 8; i < 16; i++) {
            VideoSynopsisBean bean = new VideoSynopsisBean();
            bean.setImgUrl(durls.get(i));
            bean.setSubtitle("");
            bean.setTitle(titles.get(i));
            items.add(new HomeItem(HomeItem.TYPE_1, bean));
        }

        //列表图片
        List<VideoSynopsisBean> videoSynopsisBeans2 = new ArrayList<>();
        for (int i = 5; i < 10; i++) {
            VideoSynopsisBean bean = new VideoSynopsisBean();
            bean.setImgUrl(lurls.get(i));
            bean.setSubtitle(ltitles.get(i));
            bean.setTitle(ltitles.get(i));
            videoSynopsisBeans2.add(bean);
        }
        items.add(new HomeItem(HomeItem.TYPE_2, videoSynopsisBeans2));
        return items;
    }

    @Override
    public void onInteraction(int type, Object o) {
        switch (type) {
            case HomeItem.TYPE_0:
                if (o instanceof ADBean) {
                    Logger.i("点击Item 类型为 0");
                } else {
                    Logger.i("点击搜索按钮 类型为 0");
                }
                startPlayerActivity("0");
                break;
            case HomeItem.TYPE_1:
                if (o instanceof VideoSynopsisBean) {
                    Logger.i("点击Item 类型为 1");
                }
                startPlayerActivity("2");
                break;
            case HomeItem.TYPE_2:
                if (o instanceof VideoSynopsisBean) {
                    Logger.i("点击Item 类型为 2");
                }
                startPlayerActivity("3");
                break;
        }
    }
}
